#include "v4lconvert.h"

/**
 * Versao Original da conversao YUV 4:2:2 para RGB24
 * */
void v4lconvert_yuyv_to_rgb24(const unsigned char *src, unsigned char *dest,
  int width, int height)
{
	int j;
	int u,v,u1,rg,v1;
	int screen_size = width*height;
	for (j = 0; j < screen_size; j += 2) {
		
		u = src[1];
		  v = src[3];
		  u1 = (((u - 128) << 7) +  (u - 128)) >> 6;
		  rg = (((u - 128) << 1) +  (u - 128) +
			((v - 128) << 2) + ((v - 128) << 1)) >> 3;
		  v1 = (((v - 128) << 1) +  (v - 128)) >> 1;
		  
		  *dest++ = CLIP(src[0] + v1);
		  *dest++ = CLIP(src[0] - rg);
		  *dest++ = CLIP(src[0] + u1);

		  *dest++ = CLIP(src[2] + v1);
		  *dest++ = CLIP(src[2] - rg);
		  *dest++ = CLIP(src[2] + u1);
		  src += 4;
    }
  
}
/**
 * Versao da conversao YUV 4:2:2 para RGB24 utilizando table look up
 * */
void v4lconvert_yuyv_to_rgb24_table(const unsigned char *src, unsigned char *dest,float *y, float *u_green,
	float *u_blue, float *v_red, float *v_green, int width, int height)
{
	int j;
	int u,v,u1,rg,v1;
	int screen_size = width*height;
    for (j = 0; j < screen_size; j += 2) {
		  *dest++ = CLIP(y[src[0]] + v_red[src[3]]);
		  *dest++ = CLIP(y[src[0]] - u_green[src[1]] - v_green[src[3]]);
		  *dest++ = CLIP(y[src[0]] + u_blue[src[1]]);

		  *dest++ = CLIP(y[src[2]] + v_red[src[3]]);
		  *dest++ = CLIP(y[src[2]] - u_green[src[1]] - v_green[src[3]]);
		  *dest++ = CLIP(y[src[2]] + u_blue[src[1]]);
		  
		  src += 4; 
    }
}
